package com.frank.code_notes.C01_array;

import cn.hutool.core.lang.Console;
import org.springframework.util.Assert;

/**
 * @Description 26.删除排序数组中的重复项
 * @Author zhoujr
 * @Data 2024/11/25 15:18
 */
public class L026RemoveDuplicatesFromArray {

    /**
     * @Description 使用快慢指针的方法进行以为和替换
     */
    public static int removeDuplicates(int[] nums) {
        int slow = 0;
        for (int fast = 0; fast < nums.length; fast++) {
            if (nums[slow] != nums[fast]) {
                nums[++slow] = nums[fast];
            }
        }
        return slow + 1;
    }

    public static void verify(int[] nums, int[] expectedNums) {
        int k = removeDuplicates(nums); // 调用
        Console.log(k, nums);
        assert k == expectedNums.length;
        for (int i = 0; i < k; i++) {
            Assert.isTrue(nums[i] == expectedNums[i], "存在和期望值不一样的");
        }
    }

    public static void main(String[] args) {
//        verify(new int[]{1, 1, 2}, new int[]{1, 2});
        verify(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}, new int[]{0, 1, 2, 3, 4});
    }
}
